package com.ChessApps.Interface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;

import chessapps.ChessApps;
import com.ChessApps.Network.ServerChat;
import com.ChessApps.Network.ThreadClientChat;
import com.ChessApps.Network.ThreadGame;
import com.ChessApps.Network.ThreadUpdateApps;
import com.si.entite.Piece;
import com.si.entite.Player;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class Interface extends JFrame implements ActionListener {

    JList lst = new JList();
    Boolean modeServer;
    JLabel text = new JLabel("Opponent : Ready!");
    ////////////////
    /// PANNEAUX ///
    ////////////////
    Container conteneur = this.getContentPane();
    JPanel pnPrin = new JPanel(new BorderLayout());
    JPanel pnCenter = new JPanel();
    JPanel pnWest = new JPanel(new BorderLayout());
    JPanel pnEast = new JPanel(new BorderLayout());
    JPanel pnClient = new JPanel(new GridLayout(4, 2));
    JPanel pnOpponent = new JPanel(new GridLayout(1, 2));
    JPanel pnServer = new JPanel(new GridLayout(3, 1));
    JPanel pnImages = new JPanel(new GridLayout(4, 4));
    JPanel pnInfoMoves = new JPanel();
    JPanel pnEastEast = new JPanel(new BorderLayout());
    JPanel pnWriter = new JPanel(new BorderLayout());
    ////////////////
    ///// MENU /////    
    ////////////////
    JMenu menuFile = new JMenu("File");
    JMenu menuGame = new JMenu("Game");
    JMenu menuConfig = new JMenu("Config");
    JMenu menuHelp = new JMenu("Help");
    JMenuItem menuPreference = new JMenuItem("Preference");
    JMenuItem menuNewGame = new JMenuItem("New");
    JMenuItem menuLoadGame = new JMenuItem("Load");
    JMenuItem menuSaveGame = new JMenuItem("Save");
    JMenuItem menuQuit = new JMenuItem("Quit");
    JMenuItem menuAbout = new JMenuItem("About");
    JMenuItem menuCnx = new JMenuItem("Connexion");
    JMenuBar menuBar = new JMenuBar();
    ChessApps tableDeJeu;
    //////////////////////
    // CONNEXION //
    //////////////////////
    JLabel cnxServerAdr = new JLabel();
    JLabel cnxIp = new JLabel();
    JLabel cnxExtIp = new JLabel();
    JLabel cnxPort = new JLabel();
    JLabel cnxName = new JLabel();
    JLabel opponentState = new JLabel();
    JLabel cnxHostName = new JLabel();
    int port = 9999;
    String adress = "127.0.0.1";
    Socket sock;
    ServerChat serveur;
    PrintWriter os;
    PrintWriter osGame;
    BufferedInputStream is;
    BufferedInputStream isGame;
    /////////CHAT//////////
    JScrollPane jspConversation;
    JTextArea conversation;
    JScrollPane jspWriter;
    JTextArea writer;
    ///////ECRIRE MOUVEMENT//////
    JScrollPane jspMoves;
    JTextArea moves;
    JButton ok;
    ///////PLAYER ///////////////
    Player player;
    String playerName = "John Doe";
    String side = "blanc";
    Boolean startPlay = false;
    BufferedImage myPicture;
    BufferedImage imgChat;
    int nbPlayer = 0;

    ////////////////////////
    ///// CONSTRUCTEUR /////    
    ////////////////////////
    @SuppressWarnings("LeakingThisInConstructor")
    public Interface() throws UnknownHostException {
        super("Chess Online");
        // Ajout des éléments //
        menuConfig.add(menuCnx);
        menuFile.add(menuNewGame);
        menuFile.add(menuLoadGame);
        menuFile.add(menuSaveGame);
        menuFile.add(menuQuit);
        menuHelp.add(menuAbout);
        menuGame.add(menuPreference);
        menuBar.add(menuFile);
        menuBar.add(menuGame);
        menuBar.add(menuConfig);
        menuBar.add(menuHelp);
        this.setJMenuBar(menuBar);
        cnxServerAdr.setFont(new Font("Serif", Font.BOLD, 15));
        cnxIp.setFont(new Font("Serif", Font.BOLD, 15));
        cnxPort.setFont(new Font("Serif", Font.BOLD, 15));
        //cnxExtIp.setFont(new Font("Serif", Font.BOLD, 15));
        cnxName.setFont(new Font("Serif", Font.BOLD, 15));
        cnxHostName.setFont(new Font("Serif", Font.BOLD, 15));
        opponentState.setFont(new Font("Serif", Font.BOLD, 15));


        // Bordures et noms des panneaux //
        TitledBorder tlCenter, tlEastCenter, tlEastEast, tlClient, tlServer, tlImages;
        tlCenter = BorderFactory.createTitledBorder("Game");
        tlEastCenter = BorderFactory.createTitledBorder("Game info");
        tlClient = BorderFactory.createTitledBorder("Client info");
        tlServer = BorderFactory.createTitledBorder("Server info");
        tlEastEast = BorderFactory.createTitledBorder("Chat");
        tlImages = BorderFactory.createTitledBorder("Dead Soldiers");
        pnCenter.setBorder(tlCenter);
        pnInfoMoves.setBorder(tlEastCenter);
        pnEastEast.setBorder(tlEastEast);
        pnClient.setBorder(tlClient);
        pnServer.setBorder(tlServer);
        pnImages.setBorder(tlImages);


        pnServer.add(cnxServerAdr);
        pnServer.add(cnxPort);
        pnServer.add(cnxIp);
        pnClient.add(cnxName);
        pnClient.add(cnxHostName);
//        pnClient.add(cnxExtIp);
        pnOpponent.add(opponentState);
        pnClient.add(pnOpponent);

        // info sur chat //
        conversation = new JTextArea();
        conversation.setEditable(false);
        conversation.setLineWrap(true);
        try {
            imgChat = ImageIO.read(new File("./src/com/ChessApps/images/imgChat.png"));
        } catch (IOException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }
        JViewport jvp = new JViewport() {

            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (imgChat != null) {
                    g.drawImage(imgChat, 0, 0, this.getWidth(), this.getHeight(), this);
                }
            }
        };
        conversation.setOpaque(false);

        jspConversation = new JScrollPane();
        jspConversation.setViewport(jvp);
        jspConversation.setViewportView(conversation);
        jspConversation.setPreferredSize(new Dimension(200, 400));
        writer = new JTextArea();
        writer.setLineWrap(true);
        jspWriter = new JScrollPane(writer);
        jspWriter.setPreferredSize(new Dimension(200, 70));


        jspWriter.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jspConversation.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);


        moves = new JTextArea();
        moves.setBackground(Color.LIGHT_GRAY);
        moves.setLineWrap(true);
        moves.setEditable(false);
        jspMoves = new JScrollPane(moves);
        jspMoves.setPreferredSize(new Dimension(200, 250));
        pnInfoMoves.add(jspMoves);

        moves.append("   ----------------- Moves -----------------\n");
        ok = new JButton("SEND");

        //----------------------choisir entre le serveur ou le client -----------------------------------------------------------------


        // entrer le nom du joueur
        JTextField namep = new JTextField();
        JOptionPane pane = new JOptionPane("1/2 Profil");
        pane.setMessage("Username : ");
        pane.setWantsInput(true);
        pane.setIcon(new ImageIcon("./src/com/ChessApps/images/knight.jpg"));
        pane.setInputValue(namep);
        JDialog dialog = pane.createDialog(this, "1/2 Profil");
        dialog.show();
        Object selectedValue = pane.getInputValue();

        //If there is not an array of option buttons:
        if ((selectedValue != null) && !(selectedValue instanceof JTextField)) {
            this.playerName = (String) selectedValue;
        }
        // choix entre serveur ou client    
        int rep = JOptionPane.showOptionDialog(this, "Make the right Choice...it can be the last !!!", "2/2 Network", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon("./src/com/ChessApps/images/10.gif"), new String[]{"Server", "Client"}, "default");

        player = new Player(this.playerName, side, startPlay);

        if (rep == JOptionPane.YES_OPTION) {// c'est un client/serveur
            serveur = new ServerChat(port);
            System.out.println("server On");
            side = "noir";
            startPlay = true;
            player = new Player(this.playerName, side, startPlay);

            cnxServerAdr.setText("Server Mode");
            modeServer = true;
            this.setGameInfo();
        } else {
            cnxServerAdr.setText("Server IP :" + this.getAdress());
            modeServer = false;
            this.setGameInfo();

        }



        tableDeJeu = new ChessApps(player);
        ////////////////
        ///// CHAT /////    
        ////////////////
        try {
            if (modeServer) {
                conversation.append("Waiting for a client...");
            } else {
                conversation.append("looking for a server...");
            }

            sock = new Socket(adress, port);

            is = new BufferedInputStream(sock.getInputStream());
            os = new PrintWriter(sock.getOutputStream());
            ThreadClientChat thChatClient = new ThreadClientChat(this);
            thChatClient.start();
            ThreadGame g = new ThreadGame(this);
            g.start();
            ThreadUpdateApps tua = new ThreadUpdateApps(this);
            tua.start();


            conversation.append("\nConnection established.");
            conversation.append("\n----------------------------");

        } catch (IOException e) {
            System.out.println(e);
        }

        // Organisation des panneaux //

        // Size
        pnCenter.add(tableDeJeu);
        pnEast.setPreferredSize(new Dimension(250, 650));
        pnWest.setPreferredSize(new Dimension(250, 650));
        pnEastEast.setPreferredSize(new Dimension(250, 650));
        pnImages.setPreferredSize(new Dimension(250, 300));
        pnCenter.setPreferredSize(new Dimension(650, 650));



        pnWriter.add(jspConversation, BorderLayout.NORTH);
        pnWriter.add(jspWriter, BorderLayout.CENTER);
        writer.setBackground(Color.lightGray);
        pnWriter.add(ok, BorderLayout.SOUTH);
        pnEastEast.add(pnWriter, BorderLayout.CENTER);

        pnEast.add(pnEastEast, BorderLayout.CENTER);
        pnEastEast.add(pnServer, BorderLayout.SOUTH);
        pnPrin.add(pnCenter, BorderLayout.CENTER);
        pnPrin.add(pnEast, BorderLayout.EAST);
        pnPrin.add(pnWest, BorderLayout.WEST);
        pnWest.add(pnImages, BorderLayout.NORTH);
        pnWest.add(pnInfoMoves, BorderLayout.CENTER);
        pnWest.add(pnClient, BorderLayout.SOUTH);
        conteneur.add(pnPrin);

        //-----------------------------------------------------------------------------    
        ////////////////
        /// LISTENER ///
        ////////////////
        menuCnx.addActionListener(this);
        menuQuit.addActionListener(this);
        menuAbout.addActionListener(this);
        menuLoadGame.addActionListener(this);
        menuSaveGame.addActionListener(this);
        ok.addActionListener(this);
        menuPreference.addActionListener(this);
//-----------------set de la fenetre principale---------------------------------
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(1250, 760);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);

        //si le client a decidé de fermer lapplication juste avant de commencer. eh ben ca disparait 
        if (rep == JOptionPane.CLOSED_OPTION) {
            this.dispose();
        }
    }

    public ServerChat getServeur() {
        return serveur;
    }

    //////////////
    //// MAIN ////
    //////////////
    public static void main(String args[]) throws UnknownHostException {
        Interface game = new Interface();
    }

    //////////////////////////////
    ////// ACTION PERFORMED //////
    //////////////////////////////
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == menuQuit) {

            if (JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?", "Confimation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        }
//------------------------------------------------        
        // CONFIGURATION DE CONNEXION //
        if (e.getSource() == menuCnx) {
            restartServer();
        }

//------------le boutton ok est pour le chat, pour envoyer le message au serveur------------
        if (e.getSource() == ok) {
            if ("".equals(writer.getText())) {
            } else {
                send("\n" + this.playerName + " : " + writer.getText());
                conversation.append("\nMoi : " + writer.getText());
                writer.setText("");
                writer.requestFocusInWindow();
            }
        }


//--------------------bouttons du menu-----------------------------------------------
        if (e.getSource() == menuAbout) {
            //JOptionPane.showMessageDialog(null, );
            JOptionPane.showMessageDialog(this, "\n « Le jeu d'échecs fait naître et fortifie en nous plusieurs\n"
                    + " qualités précieuses dans le cours de l'existence, telles que la prévoyance,\n"
                    + " la circonspection, la prudence, et la persévérance. » Benjamin Franklin. "
                    + "\n\n Cette application est libre de droit. \n\n "
                    + "Contact : sadekilyes@gmail.com \n\n", "Informations", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("./src/com/ChessApps/images/exclamation.png"));
        }

        // Ouvrir partie //
        if (e.getSource() == menuLoadGame) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
        }
        // Enregistrer partie //
        if (e.getSource() == menuSaveGame) {
            JFileChooser save = new JFileChooser();
            save.showSaveDialog(this);
        }
        if (e.getSource() == menuPreference) {
            // Combobox couleur //
            JComboBox j = new JComboBox();
            j.addItem("Blue");
            j.addItem("Orange");
            j.addItem("Gray");
            j.addItem("Red");
            j.addItem("Yellow");
            // Panneaux //
            JPanel pnPrinPref = new JPanel(new GridLayout(2, 1));
            JPanel pnThm = new JPanel(new FlowLayout(FlowLayout.CENTER));
            JPanel pnSons = new JPanel(new FlowLayout(FlowLayout.CENTER));
            // Ajout des éléments dans les panneaux //
            pnThm.add(new JLabel("Thème : "));
            pnThm.add(j);
            pnPrinPref.add(pnThm);
            pnPrinPref.add(pnSons);

            int r = JOptionPane.showConfirmDialog(this, pnPrinPref, "Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
            if (r == JOptionPane.YES_OPTION) {
                if (j.getSelectedItem() == "Blue") {
                    this.getTableDeJeu().changerTheme(Color.BLUE, Color.cyan);
                    //changeTheme(Color.cyan, Color.white);
                    this.repaint();
                }
                if (j.getSelectedItem() == "Orange") {
                    this.getTableDeJeu().changerTheme(Color.ORANGE, Color.white);
                    //changeTheme(Color.GRAY, Color.white);
                    this.repaint();
                }
                if (j.getSelectedItem() == "Gray") {
                    Color grisSite = new Color(78, 78, 78);
                    Color grisSite2 = new Color(98, 98, 98);
                    this.getTableDeJeu().changerTheme(grisSite, Color.LIGHT_GRAY);
                    //changeTheme(grisSite2, grisSite2);
                    this.repaint();
                }
                if (j.getSelectedItem() == "Red") {
                    this.getTableDeJeu().changerTheme(Color.red, Color.white);
                    //changeTheme(Color.cyan, Color.white);
                    this.repaint();
                }
                if (j.getSelectedItem() == "Yellow") {
                    this.getTableDeJeu().changerTheme(Color.YELLOW, Color.LIGHT_GRAY);
                    //changeTheme(Color.DARK_GRAY, Color.white);
                    this.repaint();
                }

            }
        }

    }

    public void send(String message) {
        os.print(message);
        os.flush();
    }

    public void lireSGame(String message) {
        String couleurTemp = "";
        if ((message == null) || ("".equals(message))) {
        } else {

            moves.append("Adversaire : " + message + "\n");
            String[] s = new String[2];
            String[] c = new String[2];
            String[] c1 = new String[2];
            s = message.split(":");
            c = s[0].split(",");
            c1 = s[1].split(",");

            this.getTableDeJeu().selectionner(Integer.parseInt(c[0]), Integer.parseInt(c[1]));

            couleurTemp = this.getTableDeJeu().getTable().get(this.getTableDeJeu().getTable().getPosPieceCourante().getX(), this.getTableDeJeu().getTable().getPosPieceCourante().getY()).getCouleur();

            this.getTableDeJeu().deplacement(Integer.parseInt(c1[0]), Integer.parseInt(c1[1]));

            if (!couleurTemp.equals(this.getTableDeJeu().getPlayer().getSide())) {
                this.player.setPlay(true);
            }
            this.getTableDeJeu().setMove("");
            this.updateEatenPieces();
            this.getTableDeJeu().repaint();
            couleurTemp = "";
        }
    }

    // FONCTION LIRESOCKET //
    public void lireSocket() {
        byte[] buf = new byte[200];
        String message;

        try {
            is.read(buf);
            message = (new String(buf)).trim();

            // si c'est un mouvement fait par l'adversaire alors l'executer
            if (message.contains("__move_")) {
                this.lireSGame(message.substring(7, message.length()));
                return;
            }

            //si cest un message de l'adversaire comme quoi il est pret, alors changer l'etat de ladversaire
            if ("OpponentReadyStateCharged".contains(message)) {
                this.setNbPlayer(2);
                opponentReady();
               return;
            }

            //si c'est le message du chet alors l'afficher
            if (message.contains(this.playerName)) {
                message = message.substring(this.playerName.length());
                conversation.append("\n" + message);

            } else {
                conversation.append("\n" + message);
            }


        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public void opponentReady() {

        try {
            myPicture = ImageIO.read(new File("./src/com/ChessApps/images/crochetValid.png"));
        } catch (IOException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }

        text.setFont(new Font("Serif", Font.BOLD, 15));
        this.pnOpponent.remove(opponentState);
        this.pnOpponent.add(text);
        //this.pnOpponent.add(picLabel);
    }

    public void updateEatenPieces() {
        Piece[] eatenPieces = this.getTableDeJeu().getTable().getEatenPieces();
        String chosenImage;
        pnImages.removeAll();
        for (int i = 0; i < 16; i++) {
            if (eatenPieces[i] != null) {
                if ("blanc".equals(eatenPieces[i].getCouleur())) {
                    chosenImage = eatenPieces[i].getPathImage(1);
                } else {
                    chosenImage = eatenPieces[i].getPathImage(2);
                }

                try {
                    myPicture = ImageIO.read(new File(chosenImage));
                } catch (IOException ex) {
                    Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                }
                JLabel picLabel = new JLabel(new ImageIcon(myPicture));

                pnImages.add(picLabel);

            }
        }
    }
    /////////////////////////
    //// GETTER & SETTER ////
    /////////////////////////

    public JPanel getPnCenter() {
        return pnCenter;
    }

    public PrintWriter getOs() {
        return os;
    }

    public void setOs(PrintWriter os) {
        this.os = os;
    }

    public BufferedInputStream getIs() {
        return is;
    }

    public void setIs(BufferedInputStream is) {
        this.is = is;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getNbPlayer() {
        return nbPlayer;
    }

    public void setNbPlayer(int nbPlayer) {
        this.nbPlayer = nbPlayer;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
        System.out.println("Nouvelle adresse : " + adress);
    }

    public ChessApps getTableDeJeu() {
        return tableDeJeu;
    }

    public String getIpAddress() throws MalformedURLException, IOException {
        URL myExtIP = new URL("http://api.externalip.net/ip/");
        BufferedReader in;
        in = new BufferedReader(
                new InputStreamReader(myExtIP.openStream()));
        return in.readLine();
    }

    public void setGameInfo() {
        try {
            cnxIp.setText("Local IP : " + InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            cnxHostName.setText("Hostname : " + InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException ex) {
            Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }
        opponentState.setText("Opponent : not ready! ");
        cnxPort.setText("Port : " + this.getPort());
        if ("".equals(playerName)) {
            cnxName.setText("User :  Anonym");
        } else {
            cnxName.setText("User : " + playerName);
        }
        this.repaint();


        /*try {
        String ipAddress = getIpAddress();
        cnxExtIp.setText("External IP : " + ipAddress);
        } catch (MalformedURLException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
        Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
        }*/
    }

    public void writeMoves(String s) {

        moves.append(s);
    }

//    public void changeTheme(Color c1, Color c2) {
//        this.moves.setBackground(c1);
//        this.pnCenter.setBackground(c2);
//        this.pnInfoMoves.setBackground(c2);
//        this.pnEastEast.setBackground(c2);
//        this.conversation.setBackground(c1);
//        this.writer.setBackground(Color.GRAY);
//
//    }
    public void restartServer() {
        // Panneaux //
        JPanel pnPrinCnx = new JPanel(new GridLayout(2, 1));
        JPanel pnAdrs = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JPanel pnPort = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        // Textbox //
        JTextField txBxAdrs = new JTextField(this.getAdress(), 10);
        JTextField txBxPort = new JTextField(String.valueOf(this.getPort()), 10);
        // Ajout des éléments dans les panneaux //
        pnAdrs.add(new JLabel("Server adress : "));
        if (modeServer) {
            txBxAdrs.setEditable(false);
            txBxAdrs.setToolTipText("You can't change adress of your own server.");
        }
        pnAdrs.add(txBxAdrs);
        pnPort.add(new JLabel("Server port : "));
        pnPort.add(txBxPort);
        pnPrinCnx.add(pnAdrs);
        pnPrinCnx.add(pnPort);
        int r = JOptionPane.showConfirmDialog(this, pnPrinCnx, "Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (r == JOptionPane.YES_OPTION) {
//                this.tableDeJeu = null;
//
//                Echiquier table = new Echiquier();
//                tableDeJeu = new ChessApps(player);
//                tableDeJeu.setTable(table);
//                this.pnCenter.add(tableDeJeu);

            this.setAdress(txBxAdrs.getText());
            this.setPort(Integer.parseInt(txBxPort.getText()));
            this.serveur = new ServerChat(port);
            serveur.setPort(port);
            try {
                //                is.close();
                //                os.close();
                //                sock.close();
                this.sock = new Socket(adress, port);
            } catch (UnknownHostException ex) {
                Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                is = new BufferedInputStream(sock.getInputStream());
            } catch (IOException ex) {
                Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                os = new PrintWriter(sock.getOutputStream());
            } catch (IOException ex) {
                Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
            }
            ThreadClientChat thChatClient = new ThreadClientChat(this);
            thChatClient.start();
            ThreadGame g = new ThreadGame(this);
            g.start();

        }

        conversation.setText("");
        setGameInfo();
        conversation.append("Changement effectué. \nEn attente d'une connexion. \nAdresse IP : " + adress + "\nPort : " + port);
    }
}
